﻿using System;
using StructureMap;
using WholesaleOnline.Contract;
using WholesaleOnline.Controller.Request;
using WholesaleOnline.Controller.Routing;
using WholesaleOnline.Controller.Storage;
using WholesaleOnline.DataContract.Messaging;

namespace WholesaleOnline.Controller.Handlers
{
    public class RemoveBasketItemHandler : RequestHandler
    {
        private IBasketService m_basketService = null;

        public RemoveBasketItemHandler(IHttpStorage storage, IBasketService basketService)
            : base(Routes.REMOVE_BASKET_ITEM, storage)
        {
            m_basketService = basketService;
        }

        public override void Handle(WebRequest webRequest)
        {
            if (Route.Matches(webRequest))
            {
                RemoveBasketItemRequest request = new RemoveBasketItemRequest();
                request.ItemId = webRequest.QueryArguments.ExtractFor<Guid>("item");
                m_basketService.RemoveBasketItemBy(request);

                ObjectFactory.GetInstance<IHandlerFactory>().GetHandlers().Handle(
                    ObjectFactory.GetInstance<IWebRequestFactory>().CreateFrom(Routes.GET_BASKETS));
            }
            else
            {
                base.NextHandler.Handle(webRequest);
            }
        }
    }
}
